package com.lai.qq.admin.biz.controller;


import com.alibaba.fastjson.JSON;
import com.lai.qq.admin.biz.req.AutoDisplacementReq;
import com.lai.qq.admin.biz.res.RebackInfoResult;
import com.byd.dilink.db.vehicle.api.entity.AutoDisplacement;
import com.byd.dilink.db.vehicle.api.service.AutoDisplacementDbService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@Slf4j
@RequestMapping("/api/admin/autoDisplacement")
@RestController
@Api(tags = "排量模块")
public class AutoDisplacementController {

    @Reference
    private AutoDisplacementDbService autoDisplacementDbService;


    @PostMapping("/selectAutoDisplacement")
    public RebackInfoResult selectAutoDisplacement(@RequestBody AutoDisplacementReq req) throws Exception {
        log.info("/autoDisplacement/selectAutoDisplacement,req={}", JSON.toJSONString(req));
        AutoDisplacement param = new AutoDisplacement();
        BeanUtils.copyProperties(req,param);
        AutoDisplacement data = autoDisplacementDbService.selectOne(param);
        return new RebackInfoResult().success(data);
    }

    @PostMapping("/selectAutoDisplacementList")
    public RebackInfoResult selectAutoDisplacementList(@RequestBody AutoDisplacementReq req) throws Exception {
        log.info("/autoDisplacement/selectAutoDisplacementList,req={}", JSON.toJSONString(req));
        AutoDisplacement param = new AutoDisplacement();
        BeanUtils.copyProperties(req,param);
        List<AutoDisplacement> list = autoDisplacementDbService.selectListByAutoDisplacement(param);
        return new RebackInfoResult().success(list);
    }

    @PostMapping("/insertAutoDisplacement")
    public RebackInfoResult insertAutoDisplacement(@RequestBody @Validated  AutoDisplacementReq req ,BindingResult bindingResult) throws Exception {
        log.info("/autoDisplacement/insertAutoDisplacement,req={}", JSON.toJSONString(req));
        AutoDisplacement param = new AutoDisplacement();
        BeanUtils.copyProperties(req,param);
        int count =  autoDisplacementDbService.insert(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/updateAutoDisplacement")
    public RebackInfoResult updateAutoDisplacement(@RequestBody @Validated AutoDisplacementReq req, BindingResult bindingResult) throws Exception {
        log.info("/autoDisplacement/updateAutoDisplacement,req={}", JSON.toJSONString(req));
        AutoDisplacement param = new AutoDisplacement();
        BeanUtils.copyProperties(req,param);
        int count = autoDisplacementDbService.updateByPrimaryKey(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/deleteAutoDisplacement")
    public RebackInfoResult deleteAutoDisplacement(@RequestBody AutoDisplacementReq req) throws Exception {
        log.info("/autoDisplacement/deleteAutoDisplacement,req={}", JSON.toJSONString(req));
        AutoDisplacement param = new AutoDisplacement();
        BeanUtils.copyProperties(req,param);
        int count = autoDisplacementDbService.delete(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/insertToMongoDb")
    public RebackInfoResult insertToMongoDb(@RequestBody @Validated AutoDisplacementReq req, BindingResult bindingResult) throws Exception {
        log.info("/autoDisplacement/insertToMongoDb,req={}", JSON.toJSONString(req));
        AutoDisplacement param = new AutoDisplacement();
        BeanUtils.copyProperties(req,param);
        int count =  autoDisplacementDbService.insertToMongoDb(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/updateToMongoDb")
    public RebackInfoResult updateToMongoDb(@RequestBody @Validated AutoDisplacementReq req, BindingResult bindingResult) throws Exception {
        log.info("/autoDisplacement/updateToMongoDb,req={}", JSON.toJSONString(req));
        AutoDisplacement param = new AutoDisplacement();
        BeanUtils.copyProperties(req,param);
        int count = autoDisplacementDbService.updateToMongoDb(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/deleteToMongoDb")
    public RebackInfoResult deleteToMongoDb(@RequestBody AutoDisplacementReq req) throws Exception {
        log.info("/autoDisplacement/deleteToMongoDb,req={}", JSON.toJSONString(req));
        AutoDisplacement param = new AutoDisplacement();
        BeanUtils.copyProperties(req,param);
        int count = autoDisplacementDbService.deleteToMongoDb(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/selectOneToMongoDb")
    public RebackInfoResult selectToMongoDb(@RequestBody AutoDisplacementReq req) throws Exception {
        log.info("/autoDisplacement/selectOneToMongoDb,req={}", JSON.toJSONString(req));
        AutoDisplacement param = new AutoDisplacement();
        BeanUtils.copyProperties(req,param);
        AutoDisplacement data = autoDisplacementDbService.selectOneToMongoDb(param);
        return new RebackInfoResult().success(data);
    }

}
